//
//  HJJShopViewController.m
//  Pianke
//
//  Created by 黄俊杰 on 15/12/22.
//  Copyright © 2015年 dada. All rights reserved.
//

#import "HJJShopViewController.h"
#import "LDJ_ShopDetailViewController.h"
#import "LDJ_DetailWebViewController.h"





@interface HJJShopViewController ()<LDJ_ShopTableViewCellDelegate>

@end

@implementation HJJShopViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self createTableview];
    
    
    
    
    
}

-(void)createTableview{

    self.view.backgroundColor = [UIColor whiteColor];
    self.bigArray = [NSMutableArray array];
    
    self.myTableView = [[UITableView alloc] init];
    self.myTableView.delegate = self;
    self.myTableView.dataSource = self;
    [self.view addSubview:self.myTableView];
    self.myTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.myTableView registerClass:[LDJ_ShopTableViewCell class] forCellReuseIdentifier:@"cell"];
    [self.myTableView mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.size.mas_equalTo(CGSizeMake(SCREEN_WIDTH, SCREEN_HEIGHT));
        make.top.left.mas_equalTo(0);
    }];
    
    
    [self addHeader];
    [self addFooter];
    
    
}

-(void)addHeader{
    
    MJRefreshGifHeader *header = [WWY_AppTools headerWithRefreshingBlock:^{
        self.nextPage = 0;
        self.isUpLoading = NO;
        [self GetPersonList];
        
    }];
    self.myTableView.mj_header = header;
    [self.myTableView.mj_header beginRefreshing];

    
}
-(void)addFooter{
    
    MJRefreshBackGifFooter *footer = [WWY_AppTools footerWithRefreshingBlock:^{
        
        self.nextPage += 10;
        self.isUpLoading = YES;
        [self GetPersonList];
        
    }];
    self.myTableView.mj_footer = footer;

    
}



-(void)GetPersonList{

    
    
    
    NSString *url = @"http://api2.pianke.me/pub/shop";
    NSString *body = [NSString stringWithFormat: @"start=%ld&client=2&auth=B8Y4En3h8f1DC8Z8GcHulTJxQMOuNLCNojtsUT0xcWP22NVFSh9U6fo&limit=10", self.nextPage];
    
    NSDictionary *dic = @{@"PHPSESSID":@"58ts41v1nohdn89damknm0vdt5"};
    
    if (self.isUpLoading == NO) {
        [self.bigArray removeAllObjects];
    }
    
    [LDJ_AppTools postUrl:url body:body result:LDJJSON requestStyle:LDJRequestString headerFile:dic  success:^(id result) {
        
        NSMutableArray *array = [[result objectForKey:@"data"] objectForKey:@"list"];
        
        for (NSMutableDictionary *dic in array) {
            LDJ_ShopModel *model = [[LDJ_ShopModel alloc]init];
            [model setValuesForKeysWithDictionary:dic];
            [self.bigArray addObject:model];
        }
        
        [self.myTableView.mj_header endRefreshing];
        [self.myTableView.mj_footer endRefreshing];
        [self.myTableView reloadData];
        
    } failure:^(NSError *error) {
        
        [self.myTableView.mj_header endRefreshing];
        [self.myTableView.mj_footer endRefreshing];
    }];
    
}





-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    

    return self.bigArray.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    NSString *filePath = @"cell";
    LDJ_ShopTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:filePath];
    LDJ_ShopModel *model = [self.bigArray objectAtIndex:indexPath.row];
    cell.shopTableViewCellDelegate = self;
    cell.model = model;
    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

    LDJ_DetailWebViewController *webVC = [[LDJ_DetailWebViewController alloc] init];
    LDJ_ShopModel *model = [self.bigArray objectAtIndex:indexPath.row];
    webVC.contentId = model.contentid;
    [self.navigationController pushViewController:webVC animated:YES];
    
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

    return 220 * EACH_HEIGHT;
}


-(void)sentUrlToNextController:(NSURL *)url{

    LDJ_ShopDetailViewController *detailVC = [[LDJ_ShopDetailViewController alloc] init];
    detailVC.shopUrl = url;
    [self.navigationController pushViewController:detailVC animated:YES];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
